using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.IO;
using System.Collections.ObjectModel;
using AntEater.ComponentModel.Validation;

namespace AntEater.ComponentModel.BuildElements
{

    /// <summary>
    /// Provides the root container for the elements of a Build File.
    /// </summary>
    [Serializable]
    [ChildBuildElementType(typeof(ProjectBuildElement), IsRequired = true,MaxCount=1)]
    [Icon(typeof(RootBuildElement), "AntEater.ComponentModel.Resources.BuildFile.ico")]
    public class RootBuildElement:ContainerBuildElementBase,IRootBuildElement
    {

        /// <summary>
        /// Initializes a new instance of the <see cref="T:BuildFileRoot"/> class.
        /// </summary>
        /// <param name="context">The context.</param>
        public RootBuildElement(BuildFileDefinition context)
            :base(context) {
        }

        private string _buildFilePath;

        /// <summary>
        /// Gets the build file path.
        /// </summary>
        /// <value>The build file path.</value>
        [ToolTip()]
        [ReadOnly(true)]
        [Description("The path of the Build File.")]
        public string BuildFilePath {
            get { return _buildFilePath; }
            set { _buildFilePath = value; }
        }

        /// <summary>
        /// Gets the name of the build file.
        /// </summary>
        /// <value>The name of the build file.</value>
        [Browsable(false)]
        public string BuildFileName {
            get { 
                // Parse name of build file
                int index = this.BuildFilePath.LastIndexOf(Path.DirectorySeparatorChar);
                if (index < 0) {
                    index = 0;
                }

                return this.BuildFilePath.Substring(index + 1);
            }
        }

        /// <summary>
        /// Determines whether this instance is valid.
        /// </summary>
        /// <value></value>
        /// <returns>
        /// 	<c>true</c> if this instance is valid; otherwise, <c>false</c>.
        /// </returns>
        public override bool IsValid {
            get { throw new NotImplementedException(); }
        }

        /// <summary>
        /// Validates the specified recursive.
        /// </summary>
        /// <param name="recursive">if set to <c>true</c> [recursive].</param>
        /// <returns></returns>
        public override ReadOnlyCollection<ValidationResult> Validate(bool recursive) {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Returns a <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.
        /// </returns>
        public override string ToString() {
            return this.BuildFileName;
        }

        /// <summary>
        /// Creates a new object that is a copy of the current instance.
        /// </summary>
        /// <returns>
        /// A new object that is a copy of this instance.
        /// </returns>
        public override object Clone() {
            RootBuildElement newElement = base.Clone() as RootBuildElement;

            if (this._buildFilePath != null) {
                newElement._buildFilePath = this._buildFilePath.Clone() as string;
            }
           
            return newElement;
        }
    }
}
